﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZLCMS.Business;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Data.Model;
using NHibernate;
using Senparc.Weixin.MP.Entities;
using NHibernate.Criterion;
using System.Collections.Generic;
using ZLCMS.Business.Util;
using Senparc.Weixin.MP.Helpers;
using ZLCMS.Business.WeixinService;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class EditCouponHome : System.Web.UI.Page
    {
        protected FrameHelper _frameHelper;

        protected WxMaterial _materialDb;
        protected WxMaterialNews _newsDb;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            _frameHelper = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();
            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        private void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ITransaction tran = session.BeginTransaction();

                try
                {
                    if (_materialDb == null)//新建
                    {
                        _materialDb = new WxMaterial();

                        _materialDb.Title = tb_name.Text;
                        _materialDb.MsgType = "coupon";
                        _materialDb.CreateDate = DateTime.Now;
                        _materialDb.UpdateDate = DateTime.Now;
                        _materialDb.ResponseXml = "";
                        _materialDb.Status = "1";
                        _materialDb.OwnerAccount = _userAccount;

                        session.Save(_materialDb);

                        _newsDb = new WxMaterialNews();
                        _newsDb.MaterialId = _materialDb.Id;
                        _newsDb.Title = tb_name.Text;
                        _newsDb.Description = tb_content.Text;
                        _newsDb.PicUrl = fc_pic.Value;
                        _newsDb.Url = WeixinUtil.WxCurponListHtmlUrl;//固定指向
                        _newsDb.ShowOrder = 1;
                        _newsDb.Content = "";
                        _newsDb.ContentType = "URL";
                        _newsDb.OwnerAccount = _userAccount;

                        session.Save(_newsDb);
                    }
                    else//修改
                    {
                        _materialDb.Title = tb_name.Text;
                        _materialDb.UpdateDate = DateTime.Now;

                        _newsDb.Title = tb_name.Text;
                        _newsDb.Description = tb_content.Text;
                        _newsDb.PicUrl = fc_pic.Value;

                        session.Update(_materialDb);
                        session.Update(_newsDb);
                    }

                    session.Flush();
                    tran.Commit();
                }
                catch (System.Exception ex)
                {
                    tran.Rollback();
                    _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            _frameHelper.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                navTabId = _frameHelper.NavTabId
            });
        }

        protected void InitializeGlobalValue()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    _materialDb = session.CreateCriteria<WxMaterial>().Add(Expression.Eq("MsgType", "coupon")).Add(Expression.Eq("OwnerAccount", _userAccount)).UniqueResult<WxMaterial>();

                    if (_materialDb != null)
                    {
                        _newsDb = session.CreateCriteria<WxMaterialNews>().Add(Expression.Eq("MaterialId", _materialDb.Id)).Add(Expression.Eq("OwnerAccount", _userAccount)).Add(Expression.Eq("ShowOrder", 1)).UniqueResult<WxMaterialNews>();
                    }
                }
                catch (System.Exception ex)
                {
                    _frameHelper.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
        }

        protected void InitializeContent()
        {
            //页面控件值
            if (!IsPostBack)
            {
                if (_newsDb != null)
                {
                    tb_name.Text = _newsDb.Title;
                    tb_content.Text = _newsDb.Description;
                    fc_pic.Value = _newsDb.PicUrl;
                }
            }
        }
    }
}
